package org.jeecg.modules.chvegetable.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.chvegetable.entity.FlowerInOutReports;
import org.jeecg.modules.chvegetable.entity.FlowerTagBook;
import org.jeecg.modules.chvegetable.mapper.FlowerInOutReportsMapper;
import org.jeecg.modules.chvegetable.service.IFlowerTagBookService;
import org.jeecg.modules.chvegetable.service.IReportsService;
import org.jeecg.modules.flower.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @Description: 毛产品管理
 * @Author: jeecg-boot
 * @Date: 2022-12-06
 * @Version: V1.0
 */
@Service
@Slf4j
public class ReportsServiceImpl extends ServiceImpl<FlowerInOutReportsMapper, FlowerInOutReports> implements IReportsService {
    @Autowired
    private FlowerInOutReportsMapper flowerInOutReportsMapper;
    @Autowired
    private IFlowerTagBookService flowerTagBookService;
 
    @Override
    public JSONObject getFlowerInOutReportsInfos(Wrapper<FlowerInOutReports> wrapper,String sDate) {
        List<FlowerInOutReports> queryInInfos = flowerInOutReportsMapper.queryInInfos(wrapper, sDate);
        List<FlowerInOutReports> queryOutInfos = flowerInOutReportsMapper.queryOutInfos(wrapper, sDate);

        QueryWrapper<FlowerTagBook> flowerTagBookQueryWrapper = new QueryWrapper<>();
        flowerTagBookQueryWrapper.ge("number", 0);
        List<FlowerTagBook> bookList = flowerTagBookService.list(flowerTagBookQueryWrapper);

        BigDecimal zero = BigDecimal.ZERO;
        Map<String, FlowerInOutReports> mapToal = new HashMap<>();

        for (FlowerTagBook flowerTagBook : bookList) {
            String key = flowerTagBook.getSourceBase() + flowerTagBook.getStorehouse() + flowerTagBook.getVariety() + flowerTagBook.getProductionId()
                    + flowerTagBook.getClasses() + flowerTagBook.getPackingSpecifications() + flowerTagBook.getFlowerPoleLength();
            FlowerInOutReports flowerInOutReports = new FlowerInOutReports();
            flowerInOutReports.setSourceBase(flowerTagBook.getSourceBase());
            flowerInOutReports.setStorehouse(flowerTagBook.getStorehouse());
            flowerInOutReports.setVariety(flowerTagBook.getVariety());
            flowerInOutReports.setVarietyCode(flowerTagBook.getVarietyCode());
            flowerInOutReports.setProductionId(flowerTagBook.getProductionId());
            flowerInOutReports.setClasses(flowerTagBook.getClasses());
            flowerInOutReports.setPackingSpecifications(flowerTagBook.getPackingSpecifications());
            flowerInOutReports.setFlowerPoleLength(flowerTagBook.getFlowerPoleLength());
            flowerInOutReports.setUnitkg("kg");
            flowerInOutReports.setTagNum( flowerTagBook.getNumber());
            flowerInOutReports.setTInNum(zero);
            flowerInOutReports.setMInNum(zero);
            flowerInOutReports.setYInNum(zero);
            flowerInOutReports.setTOutNum(zero);
            flowerInOutReports.setPrice(zero);
            flowerInOutReports.setTAllPrice(zero);
            flowerInOutReports.setGetMoney(zero);
            flowerInOutReports.setMOutNum(zero);
            flowerInOutReports.setMAllPrice(zero);
            flowerInOutReports.setYOutNum(zero);
            flowerInOutReports.setYAllPrice(zero);
            mapToal.put(key, flowerInOutReports);
        }
        for (FlowerInOutReports flowerTagBook : queryInInfos) {
            String key = flowerTagBook.getSourceBase() + flowerTagBook.getStorehouse() + flowerTagBook.getVariety() + flowerTagBook.getProductionId()
                    + flowerTagBook.getClasses() + flowerTagBook.getPackingSpecifications() + flowerTagBook.getFlowerPoleLength();
            FlowerInOutReports flowerInOutReports1 = mapToal.get(key);
            if (flowerInOutReports1 == null || ObjectUtils.isEmpty(flowerInOutReports1)) {
                FlowerInOutReports flowerInOutReports = new FlowerInOutReports();
                flowerInOutReports.setSourceBase(flowerTagBook.getSourceBase());
                flowerInOutReports.setStorehouse(flowerTagBook.getStorehouse());
                flowerInOutReports.setVariety(flowerTagBook.getVariety());
                flowerInOutReports.setVarietyCode(flowerTagBook.getVarietyCode());
                flowerInOutReports.setProductionId(flowerTagBook.getProductionId());
                flowerInOutReports.setClasses(flowerTagBook.getClasses());
                flowerInOutReports.setPackingSpecifications(flowerTagBook.getPackingSpecifications());
                flowerInOutReports.setFlowerPoleLength(flowerTagBook.getFlowerPoleLength());
                flowerInOutReports.setUnitkg("kg");
                flowerInOutReports.setTagNum(zero);
                flowerInOutReports.setTInNum(flowerTagBook.getTInNum());
                flowerInOutReports.setMInNum(flowerTagBook.getMInNum());
                flowerInOutReports.setYInNum(flowerTagBook.getYInNum());
                flowerInOutReports.setTOutNum(zero);
                flowerInOutReports.setPrice(zero);
                flowerInOutReports.setTAllPrice(zero);
                flowerInOutReports.setGetMoney(zero);
                flowerInOutReports.setMOutNum(zero);
                flowerInOutReports.setMAllPrice(zero);
                flowerInOutReports.setYOutNum(zero);
                flowerInOutReports.setYAllPrice(zero);
                mapToal.put(key, flowerInOutReports);
            } else {

                flowerInOutReports1.setTInNum(flowerInOutReports1.getTInNum().add(flowerTagBook.getTInNum()));
                flowerInOutReports1.setMInNum(flowerInOutReports1.getMInNum().add(flowerTagBook.getMInNum()));
                flowerInOutReports1.setYInNum(flowerInOutReports1.getYInNum().add(flowerTagBook.getYInNum()));
                mapToal.put(key, flowerInOutReports1);
            }
        }
        for (FlowerInOutReports flowerTagBook : queryOutInfos) {
            String key = flowerTagBook.getSourceBase() + flowerTagBook.getStorehouse() + flowerTagBook.getVariety() + flowerTagBook.getProductionId()
                    + flowerTagBook.getClasses() + flowerTagBook.getPackingSpecifications() + flowerTagBook.getFlowerPoleLength();
            FlowerInOutReports flowerInOutReports1 = mapToal.get(key);
            if (flowerInOutReports1 == null || ObjectUtils.isEmpty(flowerInOutReports1)) {
                FlowerInOutReports flowerInOutReports = new FlowerInOutReports();
                flowerInOutReports.setSourceBase(flowerTagBook.getSourceBase());
                flowerInOutReports.setStorehouse(flowerTagBook.getStorehouse());
                flowerInOutReports.setVariety(flowerTagBook.getVariety());
                flowerInOutReports.setVarietyCode(flowerTagBook.getVarietyCode());
                flowerInOutReports.setProductionId(flowerTagBook.getProductionId());
                flowerInOutReports.setClasses(flowerTagBook.getClasses());
                flowerInOutReports.setPackingSpecifications(flowerTagBook.getPackingSpecifications());
                flowerInOutReports.setFlowerPoleLength(flowerTagBook.getFlowerPoleLength());
                flowerInOutReports.setUnitkg("kg");
                flowerInOutReports.setTagNum(zero);
                flowerInOutReports.setTInNum(zero);
                flowerInOutReports.setMInNum(zero);
                flowerInOutReports.setYInNum(zero);
                flowerInOutReports.setTOutNum(flowerTagBook.getTOutNum());
                flowerInOutReports.setPrice(flowerTagBook.getPrice());
                flowerInOutReports.setTAllPrice(flowerTagBook.getTAllPrice());
                flowerInOutReports.setGetMoney(flowerTagBook.getTAllPrice());
                flowerInOutReports.setMOutNum(flowerTagBook.getMOutNum());
                flowerInOutReports.setMAllPrice(flowerTagBook.getMAllPrice());
                flowerInOutReports.setYOutNum(flowerTagBook.getYOutNum());
                flowerInOutReports.setYAllPrice(flowerTagBook.getYAllPrice());
                mapToal.put(key, flowerInOutReports);
            } else {
                BigDecimal tNumall = flowerInOutReports1.getTOutNum().add(flowerTagBook.getTOutNum());
                BigDecimal TAllPrice = flowerInOutReports1.getTAllPrice().add(flowerTagBook.getTAllPrice());
                //单价
//                BigDecimal price = zero;
//                if (tNumall > 0) {
//                    price = TAllPrice.divide(new BigDecimal(tNumall), 2, BigDecimal.ROUND_HALF_UP);
//                }
                flowerInOutReports1.setTOutNum(tNumall);
                flowerInOutReports1.setTAllPrice(TAllPrice);
                flowerInOutReports1.setPrice(zero);
                flowerInOutReports1.setGetMoney(TAllPrice);
                flowerInOutReports1.setMOutNum(flowerInOutReports1.getMOutNum().add(flowerTagBook.getMOutNum()));
                flowerInOutReports1.setMAllPrice(flowerInOutReports1.getMAllPrice().add(flowerTagBook.getMAllPrice()));
                flowerInOutReports1.setYOutNum(flowerInOutReports1.getYOutNum().add(flowerTagBook.getYOutNum()));
                flowerInOutReports1.setYAllPrice(flowerInOutReports1.getYAllPrice().add(flowerTagBook.getYAllPrice()));

                mapToal.put(key, flowerInOutReports1);
            }
        }
        List<FlowerInOutReports> list = new ArrayList<>(mapToal.values());
        Map<String, String> houseNames = CommonUtil.getHouseNames();
        //循环 处理仓库 、单位转KG
        for (FlowerInOutReports flowerInOutReports : list) {
            flowerInOutReports.setStorehouse(houseNames.get(flowerInOutReports.getStorehouse()));

            String packingSpecifications = flowerInOutReports.getPackingSpecifications();//原规格
            //保留数字
            BigDecimal newKGU = new BigDecimal(1);
            if (StringUtils.isBlank(packingSpecifications)){
                newKGU = new BigDecimal(1);
            }else{
                String newKGUQUzhong = packingSpecifications.replaceAll("[^\\d.]","");
                if (newKGUQUzhong == null || StringUtils.isNotBlank(newKGUQUzhong)) {
                    newKGU = new BigDecimal(newKGUQUzhong);
                    if (newKGU == null || newKGU.compareTo(zero) == 0) {
                        newKGU = new BigDecimal(1);
                    }
                }else{
                    newKGU = new BigDecimal(1);
                }
            }
            flowerInOutReports.setTagNum(newKGU.multiply(flowerInOutReports.getTagNum()));
            flowerInOutReports.setTInNum(newKGU.multiply(flowerInOutReports.getTInNum()));
            flowerInOutReports.setMInNum(newKGU.multiply(flowerInOutReports.getMInNum()));
            flowerInOutReports.setYInNum(newKGU.multiply(flowerInOutReports.getYInNum()));
            flowerInOutReports.setTOutNum(newKGU.multiply(flowerInOutReports.getTOutNum()));

            BigDecimal tOutNum = flowerInOutReports.getTOutNum();
            BigDecimal tAllPrice = flowerInOutReports.getTAllPrice();
            BigDecimal price = zero;
            if (tOutNum != null && tOutNum.compareTo(zero) != 0) {
                price = tAllPrice.divide(tOutNum, 2, BigDecimal.ROUND_HALF_UP);
            }
            flowerInOutReports.setPrice(price);

            flowerInOutReports.setMOutNum(newKGU.multiply(flowerInOutReports.getMOutNum()));
            flowerInOutReports.setYOutNum(newKGU.multiply(flowerInOutReports.getYOutNum()));
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", list);
        return jsonObject;
    }
}
